'use strict';
app.controller('chainBusinessEditCtrl',['$scope','chainBusinessEditService','$state','toaster',function($scope,chainBusinessEditService,$state,toaster){
    $scope.frontLimit=1;
    $scope.backLimit=1;
    $scope.userLimit=1;
    $scope.subjectLimit=1;
    $scope.path=$scope.app.image.chainInfo;
    $scope.frontImages=new Array();
    $scope.frontImgQueue=new Array();
    $scope.backImages=new Array();
    $scope.backImgQueue=new Array();
    $scope.userImages=new Array();
    $scope.userImgQueue=new Array();
    $scope.subjectImages=new Array();
    $scope.subjectImgQueue=new Array();


    //证件类型
    $scope.identityType=[
        {status:'0',value:'身份证'},
        {status:'1',value:'港澳居民来往内地居住证'},
        {status:'2',value:'台胞证'},
        {status:'3',value:'护照'},
    ]

    //主体资质
    $scope.subjectType=[
        {status:'0',value:'营业执照'},
        {status:'1',value:'灵活就业（营业）辅导证/意见'},
        {status:'2',value:'事业单位法人证书'},
        {status:'3',value:'民办非企业单位登记证书'},
        {status:'4',value:'社会团体法人登记证书'}
    ]

    $scope.init=function(){
        $scope.frontLimit=1;
        $scope.frontImages=new Array();

        $scope.backLimit=1;
        $scope.backImages=new Array();

        $scope.userLimit=1;
        $scope.userImages=new Array();


        $scope.subjectLimit=1;
        $scope.subjectImages=new Array();
    }

    //查询品牌商业信息
    $scope.query=function(){
        chainBusinessEditService.query($scope).then(
            function(rlt){
                switch (rlt.status){
                    case 200:
                        $scope.init();
                        $scope.chainInfo=rlt.data;
                        if($scope.chainInfo.identityBackImgUrl!=null&&angular.isUndefined($scope.chainInfo.identityBackImgUrl)){
                            $scope.backImages.push($scope.chainInfo.identityBackImgUrl);
                            $scope.backLimit=0;
                        }
                        if($scope.chainInfo.identityFrontImgUrl!=null && !angular.isUndefined($scope.chainInfo.identityFrontImgUrl)){
                            $scope.frontImages.push($scope.chainInfo.identityFrontImgUrl);
                            $scope.frontLimit=0;
                        }

                        if($scope.chainInfo.identityImgUrl!=null && !angular.isUndefined($scope.chainInfo.identityImgUrl)){
                            $scope.userImages.push($scope.chainInfo.identityImgUrl);
                            $scope.userLimit=0;
                        }

                        if($scope.chainInfo.subjectImgUrl!=null && !angular.isUndefined($scope.chainInfo.subjectImgUrl)){
                            $scope.subjectImages.push($scope.chainInfo.subjectImgUrl);
                            $scope.subjectLimit=0;
                        }
                        break;
                    default:
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            }
        );
    }
    $scope.query();

    //删除证件正面图片
    $scope.delFrontImage=function(){
        $scope.imgType=0;
        $scope.delImg();
    }

    //删除证件反面图片
    $scope.delBackImage=function(){
        $scope.imgType=1;
        $scope.delImg();
    }

    //删除营业执照正面图片
    $scope.delUserImage=function(){
        $scope.imgType=2;
        $scope.delImg();
    }

    //删除资质照片
    $scope.delSubjectImage=function(){
        $scope.imgType=3;
        $scope.delImg();
    }

    //删除图片
    $scope.isDel=false;
    $scope.delImg=function(){
        if($scope.isDel){
            return;
        }
        $scope.isDel=true;
        chainBusinessEditService.delImg($scope).then(
            function(rlt){
                switch (rlt.status){
                    case 200:
                        toaster.pop("success","提示","操作成功");
                        $scope.findByChainId();
                        $scope.isDel=false;
                        break;
                    default:
                        toaster.pop("error","操作失败","发生未知错误！请联系管理员");
                        $scope.isDel=false;
                        break;
                }
            }
        );
    }

    //更新品牌商业信息
    $scope.saveState=false;
    $scope.updateChainInfo=function(){
        if($scope.saveState){
            return;
        }

        if($scope.frontImgQueue.length>0){
            $scope.chainInfo.identityFrontImgUrl=$scope.frontImgQueue[0].fileName;
        }else{
            delete $scope.chainInfo.identityFrontImgUrl;
        }

        if($scope.backImgQueue.length>0){
            $scope.chainInfo.identityBackImgUrl=$scope.backImgQueue[0].fileName;
        }else{
            delete $scope.chainInfo.identityBackImgUrl;
        }

        if($scope.userImgQueue.length>0){
            $scope.chainInfo.identityImgUrl=$scope.userImgQueue[0].fileName;
        }else{
            delete $scope.chainInfo.identityImgUrl;
        }

        if($scope.subjectImgQueue.length>0){
            $scope.chainInfo.subjectImgUrl=$scope.subjectImgQueue[0].fileName;
        }else{
            delete $scope.chainInfo.subjectImgUrl;
        }

        delete $scope.chainInfo.businessTerm;


        $scope.saveState=true;
        chainBusinessEditService.updateChainInfo($scope).then(
            function(rlt){
                switch (rlt.status){
                    case 200:
                        toaster.pop('success',"提示","操作成功");
                        $state.go('app.chainDatum');
                        $scope.saveState=false;
                        break;
                    default:
                        toaster.pop('error',"操作失败","发生未知错误!请联系管理员");
                        $scope.saveState=false;
                        break;
                }
            }
        );
    }
}]);